const app = getApp()
Page({
  data: {
    createTime: '', //创建时间
    expirationDate: '', // 过期时间
    state: 0, //状态 0未过期 1过期
    cardAndFaceNumber: {
      landlord: 0, //业主卡槽
      tenants: 0, //租客卡槽
      total: 0, //总卡槽
    },
    roomId: '', //房号id 用来查询当前卡槽使用情况
    addFace: {
      show: false,
      step: 0, // 步骤 0 输入使用人阶段 1拍照阶段 2拍照结束阶段
      src: '',
      member_room_id: '',
      shoot: false, // 拍照状态 是否同意
      remark: '',//使用人
      user_id: ''
    },
    isLogin: false // 是否登录
  },
  // 打开添加人脸
  openAddFace(e) {
    this.setData({ 'addFace.show': true, 'addFace.step': 0, 'addFace.src': '', 'addFace.shoot': false, 'addFace.remark': '' })
  },
  // 是否同意协议
  addFaceShootChange() {
    let state = this.data.addFace.shoot;
    this.setData({ 'addFace.shoot': !state })
  },
  // 拍照
  takePhoto() {
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({ 'addFace.src': res.tempImagePath, 'addFace.step': 2 });
      }
    })
  },
  // 添加人脸使用人更新
  addFaceRemarkChange(e) {
    this.setData({ 'addFace.remark': e.detail.value })
  },
  // 去拍照阶段
  goAddFaceCamera() {
    this.setData({ 'addFace.step': 1 });
  },
  // 去添加使用人阶段
  goAddFaceRemark() {
    this.setData({ 'addFace.step': 0 });
  },
  // 重拍
  cameraRetake() {
    this.setData({ 'addFace.step': 1, 'addFace.src': '', });
  },
  // 上传人脸 
  uploadFace() {
    app.uploadFace(this.data.addFace.src, (src) => {
      let data = { member_room_id: this.data.addFace.member_room_id, face: src, remark: this.data.addFace.remark, user_id: this.data.addFace.user_id }
      app.call.request('user/bindCard', data, (res) => {
        app.showTip('添加照片成功', 'none', () => {
          setTimeout(() => {
            this.setData({ 'addFace.show': false })
            this.getCardAndFaceNumber();
          }, 1000);
        });
      });
    });
  },
  // 获取卡槽数量
  getCardAndFaceNumber() {
    app.call.getData('user/getRoomCardFaceNum?room_id=' + this.data.roomId, (res) => {
      this.setData({
        cardAndFaceNumber: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 未登录 隐藏回到主页按钮 并且保存是否登录状态
    let authorization = wx.getStorageSync("authorization");
    if (authorization == "") {
      wx.hideHomeButton();
      this.setData({
        isLogin: false
      })
    } else {
      this.setData({
        isLogin: true
      })
    }

    let state = 0;  // 当前时间大于过期时间 则过期
    if (new Date(options.expirationDate.replace(/-/g, "/")).getTime() < new Date().getTime()) state = 1;
    this.setData({
      createTime: options.createTime,
      expirationDate: options.expirationDate,
      state,
      'addFace.member_room_id': options.memberRoomId,
      'addFace.user_id': options.userId,
      roomId: options.roomId
    })
    this.getCardAndFaceNumber()
  },

})